import redis

class RedisCon:
    def __init__(self):
        self.conn = redis.Redis(host='localhost', port=6379, db=1)
    #字符串存
    def str_set(self,key,value,time=0):
        if time == 0:
            return self.conn.set(key,value)
        else:
            #设置过期时间，单位是秒
            return self.conn.setex(key,time,value)

    #字符串读取
    def str_get(self,key):
        value = self.conn.get(key)
        if value:
            value = str(value,encoding='utf8')
        return value

    #删除
    def str_del(self,key):
        self.conn.delete(key)


r = RedisCon()
# r.str_set('name','123',30)
# print(r.str_get('name'))
